package cn.javaxc.model.base;
import cn.javaxc.mvcwork.controller.MVCConstant;
import cn.javaxc.mvcwork.controller.web.PagedListInfo;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.mvcwork.controller.MVCConfig;
import java.sql.*;
import java.util.*;
import cn.javaxc.mvcwork.util.common.MapListUtil;



/**
 *	框架Sql执行后的元数据列表<br/>
 *	列表中的所有类型均为HQLElement<br>
 *	通过元数据列表构建页面的引用框,下拉框,单选框,多选框....等
 */
public class SQLList<E extends SQLMap> extends ArrayList<E>{
  protected String tableName = null;
  protected String jsEvent = null;
  protected int selectSize = 1;
  protected String selectStyle = null;
  protected boolean multiple = false;
  protected String selectNone = "－－无－－";

  protected PagedListInfo pagedListInfo = null;
  protected HashMap selectedItems = null;
  protected Map namesMap=new HashMap();
  protected List<String> columnNames=new ArrayList();
  protected TreeBean treeBean;

  public SQLList(){
  
  }
  public SQLList(Map m){
	for (Object k:m.keySet()){
		appendItem(k,""+m.get(k));
	}
  }
  public SQLMap createElement(){
	return new SQLMap();
  }
  /**
   *  Gets the pagedListInfo attribute of the SQLList object
   *
   * @return    The pagedListInfo value
   */
  public PagedListInfo getPagedListInfo() {
    return pagedListInfo;
  }


  /**
   *  Sets the pagedListInfo attribute of the SQLList object
   *
   * @param  pagedListInfo  The new pagedListInfo value
   */
  public void setPagedListInfo(PagedListInfo pagedListInfo) {
    this.pagedListInfo = pagedListInfo;
  }



  public HashMap getSelectedItems() {
    return selectedItems;
  }



  public void setSelectedItems(HashMap tmp) {
    this.selectedItems = tmp;
  }



  public void setTable(String tmp) {
    this.tableName = tmp;
  }


  public void setTableName(String tmp) {
    this.tableName = tmp;
  }



  public void setMultiple(boolean multiple) {
    this.multiple = multiple;
  }



  public void setJsEvent(String tmp) {
    this.jsEvent = tmp;
  }



  public void setSelectSize(int tmp) {
    this.selectSize = tmp;
  }



  public void setSelectStyle(String tmp) {
    this.selectStyle = tmp;
  }



  public String getTableName() {
    return tableName;
  }




  public String getTable() {
    return tableName;
  }



  public boolean getMultiple() {
    return multiple;
  }

	public String getJsEvent(){
		return this.jsEvent;
	}
	public int getSelectSize(){
		return this.selectSize;
	}
	public String getSelectStyle(){
		return this.selectStyle;
	}

	public String getSelectNone(){
		return this.selectNone;
	}
	public void setSelectNone(String selectNone){
		this.selectNone=selectNone;
	}

	  public SQLList buildTree(TreeBean tb,Object pid){
		return MVCConfig.getTreeHelper().buildTree(this,tb,pid,0);
	  }
	  public SQLList buildTop(TreeBean tb,Object pid){
		return MapListUtil.buildTop(this,tb,pid);
	  }
  public void buildTable(String filter)  {
	  String sql=null;
	if (filter==null||"".equals(filter)){
		if (tableName!=null&&tableName.length()>0){
			sql="SELECT * from "+tableName;		
		}
	}else{
		sql=filter;
	}
	buildList(sql,"",null);
  }

   /**
	*  <font color='red' size='4' style='font-weight:900'>*</font>通过Sql创建元数据列表
	*  
	*
	* @param  filter	为 元数据sql
	* @param  cntSql	为 元数据总数sql(分页时必须有,不分页时为"")
	* @param  namesMap	为 元数据sql的字段映射表
	* @param  reqParasMap	为 元数据sql的字段值表,非必须
	* @throws  SQLException  Description of the Exception
	*/
	public void buildList(String filter,String cntSql,Map reqParasMap) {
	
	}
	


	







  /**
	* <font color='red' size='4' style='font-weight:900'>*</font>从元数据列表中获得Description相同的HQLElement
	*/
  public SQLMap get(String value) {
    for(Object obj:this){
		SQLMap thisElement=(SQLMap)obj;
      if (value.equals(thisElement.getDescription())) {
        return thisElement;
      }
    }
    return null;
  }
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>从元数据列表中获得Code相同的HQLElement
	 */
  public SQLMap fromCode(Object value) {
    for(Object obj:this){
		SQLMap thisElement=(SQLMap)obj;
      if ((value+"").equals(thisElement.getCode())) {
        return thisElement;
      }
    }
    return null;
  }

  public String getDescByCode(Object value) {
    for(Object obj:this){
		SQLMap thisElement=(SQLMap)obj;
      if ((value+"").equals(thisElement.getCode())) {
        return thisElement.getDescription();
      }
    }
    return "";
  }

  /**
   *  Adds a feature to the Item attribute of the SQLList object
   *
   * @param  tmp1  The feature to be added to the Item attribute
   * @param  tmp2  The feature to be added to the Item attribute
   */

  public void pushItem(Object tmp1, String tmp2) {
    if (!exists(tmp1)) {
      SQLMap thisElement = createElement();
      thisElement.setCode(tmp1);
      thisElement.setDescription(tmp2);
      if (this.size() > 0) {
        this.add(0,(E)thisElement);
      } else {
        this.add((E)thisElement);
      }
    }
  }

  

  /**
   *  Description of the Method
   *
   * @param  tmp1  Description of Parameter
   * @param  tmp2  Description of Parameter
   */
  public void appendItem(Object tmp1,String tmp2) {
    appendItem(tmp1,tmp2,null);
  }
  public void appendItem(Object tmp1,String tmp2,Map propertiesMap) {
    SQLMap thisElement = createElement();
    thisElement.setCode(tmp1);
    thisElement.setDescription(tmp2);
	if (propertiesMap!=null){
		thisElement.putAll(propertiesMap);	
	}
    this.add((E)thisElement);
  }


  /**
   *  A group is for visual presentation only, the following items will be in
   *  this group.
   *
   * @param  category  The feature to be added to the Group attribute
   */
  public void addGroup(String category) {
    SQLMap thisElement = createElement();
    thisElement.setDescription(category);
    this.add((E)thisElement);
  }


/**
 * <font color='red' size='4' style='font-weight:900'>*</font>在元数据列表是否存在id列值
 */


 public boolean exists(Object tmp1) {
    for(Object obj:this){
	  SQLMap thisElement=(SQLMap)obj;
      if (tmp1.equals(thisElement.getCode())) {
        return true;
      }
    }
    return false;
  }


  



  public Map getNamesMap(){
		return this.namesMap;
	}
	public void setNamesMap(Map namesMap){
		this.namesMap=namesMap;
	}

	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>返回元数据列表的第一个,如果为空增加一个空记录
	 */
	public SQLMap getExistfirst(){
		if (size()>0&&get(0)!=null){
			return (SQLMap)get(0);
		}
		return createElement();
	}
	public List<String> getColumnNames(){
		return this.columnNames;
	}
	public void setColumnNames(List<String> columnNames){
		this.columnNames=columnNames;
	}	


	public Object buildValue(String cntSql,Map reqParasMap) {
		return null;
	}


	public void buildPageList(){
		String desc=getExistfirst().getDescription();
		clear();		
		String[] v_arr=desc.split("\\[page\\]");
		for (int i=0;i<v_arr.length ;i++ ){
			appendItem(""+i,v_arr[i],null);
		}
	}

	public List getList(){
		return this;
	}
	public SQLMap getMapElement(){
		Map m=new HashMap();
		for (SQLMap emt:this ){
			m.put(emt.getCode(),emt.getDescription());
		}
		SQLMap emt=new SQLMap();
		emt.putAll(m);
		return emt;
	}
	public List<Map<String,Object>> getMapList(){
		List<Map<String,Object>> list=new ArrayList();
		for (SQLMap emt:this ){
			list.add(new HashMap(emt));
		}
		return list;
	}
	public List getCodeList(){
		List list=new ArrayList();
		for (SQLMap emt:this ){
			list.add(emt.getCode());
		}
		return list;
	}
	public String getLength(){
		return size()+"";
	}

	public TreeBean getTreeBean(){
		return this.treeBean;
	}
	public void setTreeBean(TreeBean treeBean){
		this.treeBean=treeBean;
	}
}
